package com.csu.ourpetstore1.service;

import com.csu.ourpetstore1.domain.Account;
import com.csu.ourpetstore1.domain.DailyRecord;
import com.csu.ourpetstore1.persistence.AccountMapper;
import com.csu.ourpetstore1.persistence.DailyRecordMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class AccountService {
    @Autowired
    AccountMapper AccountMapper;
    @Autowired
    DailyRecordMapper dailyRecordMapper;
    //获取所有用户
    public List<Account> getAllAccount(){
        return AccountMapper.getAllAccount();
    }
    //根据用户名获取用户，一般用于查看该用户是否存在和查看用户详细信息
    public Account getAccount(String username) {
        return AccountMapper.getAccountByUsername(username);
    }
    //根据用户名和密码获取用户，一般用于登录操作
    public Account getAccount(String username, String password) {
      Account account = new Account();
      account.setUsername(username);
      account.setPassword(password);
      return AccountMapper.getAccountByUsernameAndPassword(account);
    }
    //增加用户
    public void AddAccount(Account account) {
      AccountMapper.insertAccount(account);
      AccountMapper.insertProfile(account);
      AccountMapper.insertSignon(account);
      DailyRecord dailyRecord = new DailyRecord();
        dailyRecord.setDate(new Date());
      dailyRecord.setUserid(account.getUsername());
      dailyRecord.setEvent("注册成功");
      dailyRecordMapper.insert_DailyRecord(dailyRecord);
    }
    //编辑个人信息
    public void updateAccount(Account account) {
      AccountMapper.updateAccount(account);
      AccountMapper.updateProfile(account);

      if (account.getPassword() != null && account.getPassword().length() > 0) {
        AccountMapper.updateSignon(account);
      }
    }
    //修改密码
    public void updatePassword(String username,String password){
        Account account = new Account();
        account.setUsername(username);
        account.setPassword(password);
        AccountMapper.updateSignon(account);
    }
}
